package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.LemonlaActorMapper;
import org.linlinjava.litemall.db.dao.LemonlaSessionMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LemonlaSessionService {
    @Resource
    private LemonlaSessionMapper sessionMapper;

    public void add(LemonlaSession session) {
        session.setAddTime(LocalDateTime.now());
        session.setUpdateTime(LocalDateTime.now());
        sessionMapper.insertSelective(session);
    }

    public List<LemonlaSession> queryActive(Integer userId, Integer actorId) {
        LemonlaSessionExample example = new LemonlaSessionExample();
        example.or().andStatusEqualTo((byte) 0).andDeletedEqualTo(false).andUserIdEqualTo(userId).andActorIdEqualTo(actorId);
        example.orderBy("add_time desc");
        return sessionMapper.selectByExample(example);
    }

    public int updateById(LemonlaSession session) {
        session.setUpdateTime(LocalDateTime.now());
        return sessionMapper.updateByPrimaryKeySelective(session);
    }

    public LemonlaSession query(Integer sessionId) {
        LemonlaSessionExample example = new LemonlaSessionExample();
        example.or().andStatusEqualTo((byte) 0).andDeletedEqualTo(false).andIdEqualTo(sessionId);
        return sessionMapper.selectOneByExample(example);
    }
}
